package com.sihan.swvp.sync.video;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.sihan.swvp.common.CommonUtil;
import com.sihan.swvp.entity.video.VideoSyn;
import com.sihan.swvp.service.sys.LogService;
import com.sihan.swvp.service.sys.SyncService;
import com.sihan.swvp.service.sys.VideoAutoMatchService;
import com.sihan.swvp.sync.Synchronizer;

/**
 * 视频管理器
 * @author jch
 *
 */
public class MatchSynchronizer extends Synchronizer{
	/**
	 * 最大时间范围
	 */
	public int maxdays = 30;
	
	@Autowired
	public SyncService syncService;
	@Autowired
	public VideoAutoMatchService videoMatchService;
	@Autowired
	public LogService logService;


	@Override
	public void sync() {
    	Date syncDate = new Date();
    	System.out.println("开始同步数据...");
    	try {
        	//查询原始视频
    		List<VideoSyn> videos = videoMatchService.getNoMatchsVideo(null);
    		for (VideoSyn video : videos) {
				if(null != video.getTask_id()){
					try {
						videoMatchService.addTaskVideo(video);
					} catch (Exception e) {
						// TODO: handle exception
						e.printStackTrace();
					}
				}
				else if(null != video.getRole_id() && null != video.getStarttime()){
					try {
						List<Map<String,Object>> list = videoMatchService.getTaskByRole(video.getRole_id(),video.getStarttime(),video.getEndtime());
						if(list.size() == 1){
							Map<String,Object> map = list.get(0);
							video.setTask_id(map.get("task_id").toString());
							video.setBz_id(Integer.parseInt(map.get("bz_id").toString()));
							videoMatchService.addTaskVideo(video);
						}
					} catch (Exception e) {
						// TODO: handle exception
						e.printStackTrace();
					}
				}
				else{
					/*videoMatchService.changeMatchState(video, true);
					logService.addSystemLog("视频任务关联失败", "视频关联", 0, "视频【",video.getRawpath(),"】,没有找到对应的任务,关联失败。");*/
					
					try {
						String sbid = video.getCjsb();
						if(null != sbid && !"".equals(sbid)){
							//shows
							List<Map<String,Object>> list = videoMatchService.getTaskBySb(sbid,video.getStarttime(),video.getEndtime());
							if(!list.isEmpty()){
								Map<String,Object> map = list.get(list.size()-1);
								String rwid = String.valueOf(map.get("taskid"));
								String jsid = String.valueOf(map.get("jsid"));
								int bzid = CommonUtil.o2int(map.get("bz_id").toString());
								video.setTask_id(rwid);
								video.setBz_id(bzid);
								video.setRole_id(CommonUtil.o2int(jsid));
								video.setStage_id(-1);
								videoMatchService.addTaskVideo(video);
								
								
								List<Map<String, Object>> jsVideos = videoMatchService.getJsVideos(rwid, jsid);
								List<Map<String,Object>> jsGcs = videoMatchService.getGcByRw(rwid);
								
								int videoindex = 0;
								for (int i = 0; i < jsGcs.size(); i++) {
									/*Map gc = jsGcs.get(i);
									Map<String, Object> v = jsVideos.get(i);
									videoMatchService.updateTaskVideo(CommonUtil.o2intString(v.get("vmid")),CommonUtil.o2intString(gc.get("id")));*/
									Map gc = jsGcs.get(i);
									if(videoindex < jsVideos.size()){
										Map<String, Object> bv = null;
										for ( ;videoindex < jsVideos.size(); videoindex++) {
											Map<String, Object> v = jsVideos.get(videoindex);
											System.out.println(v.get("starttime").getClass().getName());
											if(bv == null){
												videoMatchService.updateTaskVideo(CommonUtil.o2intString(v.get("vmid")),CommonUtil.o2intString(gc.get("id")));
											}
											else{
												if(null != bv.get("endtime") && v.get("starttime") != null){
													Timestamp b_end = (Timestamp)bv.get("endtime");
													Timestamp v_start =(Timestamp)v.get("starttime");
													long dif = Math.abs(b_end.getTime() - v_start.getTime())/1000;
													if(dif < 5){
														videoMatchService.updateTaskVideo(CommonUtil.o2intString(v.get("vmid")),CommonUtil.o2intString(gc.get("id")));
													}
													else{
														break;
													}
												}
												else{
													break;
												}
											}
											
											bv = v;
										}
									}
									else{
										break;
									}
								}
							}
						}
					} catch (Exception e) {
						// TODO: handle exception
						e.printStackTrace();
					}
					
					
				}
				
				//对应视频阶段
				videoMatchService.changeMatchState(video, true);
			}
    		
        	syncService.updateSyncTime(jobid, syncDate,"成功",true);
		} catch (Exception e) {
			e.printStackTrace();
			syncService.updateSyncTime(jobid, syncDate,"失败，"+e.getMessage(),false);
			
			throw e;
		}
	
	}



	@Override
	public void sync(Date starttime, Date endtime) {
		// TODO Auto-generated method stub
		sync();
	}
}
